
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import users

from datetime import timedelta
from datetime import datetime

import random,re
import os

import urllib,urllib2
from xml.sax.saxutils import escape

from models.video import Video
from models.article import Article

_DEBUG=True

class Methods():
  namelist = [u'anime',u'comic',u'game']
  @staticmethod
  def delete():
      for element in elementList:
          element.delete();
      
  



class BaseHandler(webapp.RequestHandler):
  def render(self, template_name, template_values={}):

    user = users.get_current_user()

    if user:
      log_in_out_url = users.create_logout_url(self.request.uri)
    else:
      log_in_out_url = users.create_login_url(self.request.path)

    values = {'user': user,
              'items':Methods.namelist,
              'log_in_out_url': log_in_out_url}
    values.update(template_values)

    directory = os.path.dirname(__file__)
    path = os.path.join(directory, 'templates', template_name)

    self.response.out.write(template.render(path, values, debug=_DEBUG))

class Index(BaseHandler):
    def get(self):
        articles=Article.all()

        articles_string = []

        for article in articles:
            a.keyname = a.key().name()
            articles_string.append(a) 
        template_values = {'articles_string':articles_string}
        self.render('articles.html',template_values)

class VideoPlayer(BaseHandler):
    
    def get(self):
        articles=Article.all()

        articles_string = []

        for article in articles:
            a.keyname = a.key().name()
            articles_string.append(a) 
        template_values = {'articles_string':articles_string}
        self.render('articles.html',template_values)

def main():
    application = webapp.WSGIApplication([('/', Index),
                                          (r'/videos/(.*)/(.*)', VideoPlayer)],
                                         debug=True)
    
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()
